package com.qf.j2112.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.qf.j2112.po.SysGoods;
import com.qf.j2112.service.SysGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.qf.j2112.mapper.SysGoodsMapper;


/**
 * 商品管理Service业务层处理
 * @author j2112
 * @date 2022-04-12
 */
@Slf4j
@Service
public class SysGoodsServiceImpl implements SysGoodsService {

    @Autowired
    private SysGoodsMapper sysGoodsMapper;

    @Autowired
    RedisTemplate redisTemplate;

    //查询商品
    @Override
    public SysGoods selectSysGoodsByGoodsId(Integer goodsId) {
        return sysGoodsMapper.selectSysGoodsByGoodsId(goodsId);
    }

    /*查询热门商品列表*/
    @Override
    public List<SysGoods> selectHotSysGoodsList() {
        //1.尝试从redis中查询数据
        Object goodsList=redisTemplate.opsForValue().get("hotGoods");
        if(goodsList!=null){
            log.info(" get 热门商品 from redis...");
            List<SysGoods> goodsList1= (List<SysGoods>) goodsList;
            return goodsList1;
        }
        //2.从数据库中查询数据
        List<SysGoods> goodsList2=sysGoodsMapper.selectHotSysGoodsList();
        log.info(" get 热门商品 from Db...");
        //3.写入redis中,设置有效期3分钟
        redisTemplate.opsForValue().set("hotGoods",goodsList2,3, TimeUnit.MINUTES);
        log.info(" set 热门商品 to redis...");
        return goodsList2;
    }

    @Override
    public List<SysGoods> selectAll() {
        return sysGoodsMapper.selectAll();
    }

    @Override
    public List<SysGoods> selectAllByType(Integer typeId) {
        return sysGoodsMapper.selectAllByType(typeId);
    }

    @Override
    public int insertSysGoods(SysGoods sysGoods) {
        return sysGoodsMapper.insertSysGoods(sysGoods);
    }

    //修改商品
    @Override
    public int updateSysGoods(SysGoods sysGoods) {
        return sysGoodsMapper.updateSysGoods(sysGoods);
    }

    //批量删除商品
    @Override
    public int deleteSysGoodsByGoodsIds(Integer[] goodsIds) {
        return sysGoodsMapper.deleteSysGoodsByGoodsIds(goodsIds);
    }

    //删除商品
    @Override
    public int deleteSysGoodsByGoodsId(Integer goodsId) {
        return sysGoodsMapper.deleteSysGoodsByGoodsId(goodsId);
    }
}
